<?php

class CityAutoController extends Controller {
	
	public function actionGetData($q) {
		$criteria = new CDbCriteria();
		$criteria->limit = 10;
		$criteria->addCondition('(`py` LIKE :q OR `spy` LIKE :q)');
		$criteria->params[':q'] = "{$q}%";
		$criteria->order = 'layer ASC, id ASC';
		$data = CommonDistrict::model()->findAll($criteria);
		$str = '';
		$array = array();
		foreach($data as $one) {
			if(strpos(strtolower($one->py), strtolower($q)) !== false) {
				$pstr = $one->py;
			} else {
				$pstr = $one->spy;
			}
			$array[] = array(
				'name' => $one->name,
				'pstr' => $pstr,
				'id' => $one->id,
			);
			$str .= "{$pstr}（{$one->name}）|{$one->id}\n";	
		}
		//echo $str;
		echo json_encode($array);
	}
	
	public function actionGetId($name, $parentId = null) {
		$criteria = new CDbCriteria();
		$criteria->addCondition('`name` = :name');
		$criteria->params[':name'] = $name;
		if($parentId !== null) {
			$criteria->addCondition('`parent_id = :pid`');
			$criteria->params[':pid'] = $parentId;
		}
		$model = CommonDistrict::model()->find($criteria);
		echo $model ? $model->id : null;
	}

}

?>